home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / gdb / symfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  25.3 KB  |  958 lines

  1. /* Generic symbol file reading for the GNU debugger, GDB.
  2.    Copyright 1990, 1991 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support, using pieces from other GDB modules.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "defs.h"
  23. #include "symtab.h"
  24. #include "param.h"
  25. #include "gdbcore.h"
  26. #include "frame.h"
  27. #include "target.h"
  28. #include "value.h"
  29. #include "symfile.h"
  30. #include "gdbcmd.h"
  31. #include "breakpoint.h"
  32.  
  33. #include <obstack.h>
  34. #include <assert.h>
  35.  
  36. #include <sys/types.h>
  37. #include <fcntl.h>
  38. #include <string.h>
  39. #include <sys/stat.h>
  40.  
  41. extern int info_verbose;
  42.  
  43. extern void qsort ();
  44. extern char *getenv ();
  45.  
  46. /* Functions this file defines */
  47. static bfd *symfile_open();
  48. static struct sym_fns *symfile_init();
  49. static void clear_symtab_users_once();
  50.  
  51. /* List of all available sym_fns.  */
  52.  
  53. struct sym_fns *symtab_fns = NULL;
  54.  
  55. /* Saves the sym_fns of the current symbol table, so we can call
  56.    the right XXX_new_init function when we free it.  FIXME.  This
  57.    should be extended to calling the new_init function for each
  58.    existing symtab or psymtab, since the main symbol file and 
  59.    subsequent added symbol files can have different types.  */
  60.  
  61. static struct sym_fns *symfile_fns;
  62.  
  63. /* Allocate an obstack to hold objects that should be freed
  64.    when we load a new symbol table.
  65.    This includes the symbols made by dbxread
  66.    and the types that are not permanent.  */
  67.  
  68. struct obstack obstack1;
  69.  
  70. struct obstack *symbol_obstack = &obstack1;
  71.  
  72. /* This obstack will be used for partial_symbol objects.  It can
  73.    probably actually be the same as the symbol_obstack above, but I'd
  74.    like to keep them seperate for now.  If I want to later, I'll
  75.    replace one with the other.  */
  76.  
  77. struct obstack obstack2;
  78.  
  79. struct obstack *psymbol_obstack = &obstack2;
  80.  
  81. /* File name symbols were loaded from.  */
  82.  
  83. char *symfile = 0;
  84.  
  85. /* The modification date of the file when they were loaded.  */
  86.  
  87. long /* really time_t */ symfile_mtime = 0;
  88.  
  89. /* Structures with which to manage partial symbol allocation.  */
  90.  
  91. struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
  92.  
  93. /* Structure to manage complaints about symbol file contents.  */
  94.  
  95. struct complaint complaint_root[1] = {
  96.   {(char *)0, 0, complaint_root},
  97. };
  98.  
  99. /* Some actual complaints.  */
  100.  
  101. struct complaint oldsyms_complaint = {
  102.     "Replacing old symbols for `%s'", 0, 0 };
  103.  
  104. struct complaint empty_symtab_complaint = {
  105.     "Empty symbol table found for `%s'", 0, 0 };
  106.  
  107.  
  108. /* In the following sort, we always make sure that
  109.    register debug symbol declarations always come before regular
  110.    debug symbol declarations (as might happen when parameters are
  111.    then put into registers by the compiler).  */
  112.  
  113. static int
  114. compare_symbols (s1, s2)
  115.      struct symbol **s1, **s2;
  116. {
  117.   register int namediff;
  118.  
  119.   /* Compare the initial characters.  */
  120.   namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
  121.   if (namediff != 0) return namediff;
  122.  
  123.   /* If they match, compare the rest of the names.  */
  124.   namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  125.   if (namediff != 0) return namediff;
  126.  
  127.   /* For symbols of the same name, registers should come first.  */
  128.   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  129.       - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  130. }
  131.  
  132. /* Call sort_block_syms to sort alphabetically the symbols of one block.  */
  133.  
  134. void
  135. sort_block_syms (b)
  136.      register struct block *b;
  137. {
  138.   qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  139.      sizeof (struct symbol *), compare_symbols);
  140. }
  141.  
  142. /* Call sort_symtab_syms to sort alphabetically
  143.    the symbols of each block of one symtab.  */
  144.  
  145. void
  146. sort_symtab_syms (s)
  147.      register struct symtab *s;
  148. {
  149.   register struct blockvector *bv = BLOCKVECTOR (s);
  150.   int nbl = BLOCKVECTOR_NBLOCKS (bv);
  151.   int i;
  152.   register struct block *b;
  153.  
  154.   for (i = 0; i < nbl; i++)
  155.     {
  156.       b = BLOCKVECTOR_BLOCK (bv, i);
  157.       if (BLOCK_SHOULD_SORT (b))
  158.     sort_block_syms (b);
  159.     }
  160. }
  161.  
  162. void
  163. sort_all_symtab_syms ()
  164. {
  165.   register struct symtab *s;
  166.  
  167.   for (s = symtab_list; s; s = s->next)
  168.     {
  169.       sort_symtab_syms (s);
  170.     }
  171. }
  172.  
  173. /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
  174.    (and add a null character at the end in the copy).
  175.    Returns the address of the copy.  */
  176.  
  177. char *
  178. obsavestring (ptr, size)
  179.      char *ptr;
  180.      int size;
  181. {
  182.   register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
  183.   /* Open-coded bcopy--saves function call time.
  184.      These strings are usually short.  */
  185.   {
  186.     register char *p1 = ptr;
  187.     register char *p2 = p;
  188.     char *end = ptr + size;
  189.     while (p1 != end)
  190.       *p2++ = *p1++;
  191.   }
  192.   p[size] = 0;
  193.   return p;
  194. }
  195.  
  196. /* Concatenate strings S1, S2 and S3; return the new string.
  197.    Space is found in the symbol_obstack.  */
  198.  
  199. char *
  200. obconcat (s1, s2, s3)
  201.      char *s1, *s2, *s3;
  202. {
  203.   register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
  204.   register char *val = (char *) obstack_alloc (symbol_obstack, len);
  205.   strcpy (val, s1);
  206.   strcat (val, s2);
  207.   strcat (val, s3);
  208.   return val;
  209. }
  210.  
  211. /* Accumulate the misc functions in bunches of 127.
  212.    At the end, copy them all into one newly allocated structure.  */
  213.  
  214. #define MISC_BUNCH_SIZE 127
  215.  
  216. struct misc_bunch
  217. {
  218.   struct misc_bunch *next;
  219.   struct misc_function contents[MISC_BUNCH_SIZE];
  220. };
  221.  
  222. /* Bunch currently being filled up.
  223.    The next field points to chain of filled bunches.  */
  224.  
  225. static struct misc_bunch *misc_bunch;
  226.  
  227. /* Number of slots filled in current bunch.  */
  228.  
  229. static int misc_bunch_index;
  230.  
  231. /* Total number of misc functions recorded so far.  */
  232.  
  233. static int misc_count;
  234.  
  235. void
  236. init_misc_bunches ()
  237. {
  238.   misc_count = 0;
  239.   misc_bunch = 0;
  240.   misc_bunch_index = MISC_BUNCH_SIZE;
  241. }
  242.  
  243. void
  244. prim_record_misc_function (name, address, misc_type)
  245.      char *name;
  246.      CORE_ADDR address;
  247.      enum misc_function_type misc_type;
  248. {
  249.   register struct misc_bunch *new;
  250.  
  251.   if (misc_bunch_index == MISC_BUNCH_SIZE)
  252.     {
  253.       new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
  254.       misc_bunch_index = 0;
  255.       new->next = misc_bunch;
  256.       misc_bunch = new;
  257.     }
  258.   misc_bunch->contents[misc_bunch_index].name = name;
  259.   misc_bunch->contents[misc_bunch_index].address = address;
  260.   misc_bunch->contents[misc_bunch_index].type = misc_type;
  261.   misc_bunch->contents[misc_bunch_index].misc_info = 0;
  262.   misc_bunch_index++;
  263.   misc_count++;
  264. }
  265.  
  266. static int
  267. compare_misc_functions (fn1, fn2)
  268.      struct misc_function *fn1, *fn2;
  269. {
  270.   /* Return a signed result based on unsigned comparisons
  271.      so that we sort into unsigned numeric order.  */
  272.   if (fn1->address < fn2->address)
  273.     return -1;
  274.   if (fn1->address > fn2->address)
  275.     return 1;
  276.   return 0;
  277. }
  278.  
  279. /* ARGSUSED */
  280. void
  281. discard_misc_bunches (foo)
  282.      int foo;
  283. {
  284.   register struct misc_bunch *next;
  285.  
  286.   while (misc_bunch)
  287.     {
  288.       next = misc_bunch->next;
  289.       free (misc_bunch);
  290.       misc_bunch = next;
  291.     }
  292. }
  293.  
  294. /* INCLINK nonzero means bunches are from an incrementally-linked file.
  295.    Add them to the existing bunches.
  296.    Otherwise INCLINK is zero, and we start from scratch. */
  297. void
  298. condense_misc_bunches (inclink)
  299.      int inclink;
  300. {
  301.   register int i, j;
  302.   register struct misc_bunch *bunch;
  303.  
  304.   if (inclink)
  305.     {
  306.       misc_function_vector
  307.     = (struct misc_function *)
  308.       xrealloc (misc_function_vector, (misc_count + misc_function_count)
  309.             * sizeof (struct misc_function));
  310.       j = misc_function_count;
  311.     }
  312.   else
  313.     {
  314.       misc_function_vector
  315.     = (struct misc_function *)
  316.       xmalloc (misc_count * sizeof (struct misc_function));
  317.       j = 0;
  318.     }
  319.  
  320.   bunch = misc_bunch;
  321.   while (bunch)
  322.     {
  323.       for (i = 0; i < misc_bunch_index; i++, j++)
  324.         {
  325.       misc_function_vector[j] = bunch->contents[i];
  326. #ifdef NAMES_HAVE_UNDERSCORE
  327.       if (misc_function_vector[j].name[0] == '_')
  328.           misc_function_vector[j].name++;
  329. #endif
  330.     }
  331.       bunch = bunch->next;
  332.       misc_bunch_index = MISC_BUNCH_SIZE;
  333.     }
  334.  
  335.   if (misc_function_count + misc_count != j)        /* DEBUG */
  336.     printf_filtered ("Function counts are off!  %d + %d != %d\n",
  337.       misc_function_count, misc_count, j);
  338.  
  339.   misc_function_count = j;
  340.  
  341.   /* Sort the misc functions by address.  */
  342.  
  343.   qsort (misc_function_vector, misc_function_count,
  344.      sizeof (struct misc_function),
  345.      compare_misc_functions);
  346. }
  347.  
  348.  
  349. /* Get the symbol table that corresponds to a partial_symtab.
  350.    This is fast after the first time you do it.  In fact, there
  351.    is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
  352.    case inline.  */
  353.  
  354. struct symtab *
  355. psymtab_to_symtab (pst)
  356.      register struct partial_symtab *pst;
  357. {
  358.   register struct symtab *result;
  359.  
  360.   /* If it's been looked up before, return it. */
  361.   if (pst->symtab)
  362.     return pst->symtab;
  363.  
  364.   /* If it has not yet been read in, read it.  */
  365.   if (!pst->readin)
  366.     { 
  367.       (*pst->read_symtab) (pst);
  368.     }
  369.  
  370.   return pst->symtab;
  371. }
  372.  
  373. /* Process a symbol file, as either the main file or as a dynamically
  374.    loaded file.
  375.  
  376.    NAME is the file name (which will be tilde-expanded and made
  377.    absolute herein) (but we don't free or modify NAME itself).
  378.    FROM_TTY says how verbose to be.  MAINLINE specifies whether this
  379.    is the main symbol file, or whether it's an extra symbol file such
  380.    as dynamically loaded code.  If !mainline, ADDR is the address
  381.    where the text segment was loaded.  */
  382.  
  383. void
  384. symbol_file_add (name, from_tty, addr, mainline)
  385.      char *name;
  386.      int from_tty;
  387.      CORE_ADDR addr;
  388.      int mainline;
  389. {
  390.   bfd *sym_bfd;
  391.   asection *text_sect;
  392.   struct sym_fns *sf;
  393.   char *realname;
  394.  
  395.   sym_bfd = symfile_open (name);
  396.  
  397.   entry_point = bfd_get_start_address (sym_bfd);
  398.  
  399.   if (mainline)
  400.     symfile_mtime = bfd_get_mtime (sym_bfd);
  401.  
  402.   /* There is a distinction between having no symbol table
  403.      (we refuse to read the file, leaving the old set of symbols around)
  404.      and having no debugging symbols in your symbol table (we read
  405.      the file and end up with a mostly empty symbol table).  */
  406.  
  407.   if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
  408.     {
  409.       error ("%s has no symbol-table", name);
  410.     }
  411.  
  412.   if ((symtab_list || partial_symtab_list)
  413.       && mainline
  414.       && from_tty
  415.       && !query ("Load new symbol table from \"%s\"? ", name))
  416.     error ("Not confirmed.");
  417.  
  418.   if (from_tty)
  419.     {
  420.       printf_filtered ("Reading symbol data from %s...", name);
  421.       wrap_here ("");
  422.       fflush (stdout);
  423.     }
  424.  
  425.   sf = symfile_init (sym_bfd);
  426.   realname = bfd_get_filename (sym_bfd);
  427.   realname = savestring (realname, strlen (realname));
  428.   /* FIXME, this probably creates a storage leak... */
  429.  
  430.   if (mainline) 
  431.     {
  432.       /* Since no error yet, throw away the old symbol table.  */
  433.  
  434.       if (symfile)
  435.     free (symfile);
  436.       symfile = 0;
  437.       free_all_symtabs ();
  438.       free_all_psymtabs ();
  439.  
  440.       (*sf->sym_new_init) ();
  441.  
  442.       /* For mainline, caller didn't know the specified address of the
  443.          text section.  We fix that here.  */
  444.       text_sect = bfd_get_section_by_name (sym_bfd, ".text");
  445.       addr = bfd_section_vma (sym_bfd, text_sect);
  446.     }
  447.  
  448.   clear_complaints();    /* Allow complaints to appear for this new file. */
  449.  
  450.   (*sf->sym_read) (sf, addr, mainline);
  451.  
  452.   /* Don't allow char * to have a typename (else would get caddr_t.)  */
  453.   /* Ditto void *.  FIXME should do this for all the builtin types.  */
  454.  
  455.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  456.   TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
  457.  
  458.   if (mainline)
  459.     {
  460.       /* OK, make it the "real" symbol file.  */
  461.       symfile = realname;
  462.       symfile_fns = sf;
  463.     }
  464.  
  465.   /* If we have wiped out any old symbol tables, clean up.  */
  466.   clear_symtab_users_once ();
  467.  
  468.   if (from_tty)
  469.     {
  470.       printf_filtered ("done.\n");
  471.       fflush (stdout);
  472.     }
  473. }
  474.  
  475. /* This is the symbol-file command.  Read the file, analyze its symbols,
  476.    and add a struct symtab to symtab_list.  */
  477.  
  478. void
  479. symbol_file_command (name, from_tty)
  480.      char *name;
  481.      int from_tty;
  482. {
  483.  
  484.   dont_repeat ();
  485.  
  486.   if (name == 0)
  487.     {
  488.       if ((symtab_list || partial_symtab_list)
  489.       && from_tty
  490.       && !query ("Discard symbol table from `%s'? ", symfile))
  491.     error ("Not confirmed.");
  492.       if (symfile)
  493.     free (symfile);
  494.       symfile = 0;
  495.       free_all_symtabs ();
  496.       free_all_psymtabs ();
  497.       /* FIXME, this does not account for the main file and subsequent
  498.          files (shared libs, dynloads, etc) having different formats. 
  499.          It only calls the cleanup routine for the main file's format.  */
  500.       if (symfile_fns) {
  501.         (*symfile_fns->sym_new_init) ();
  502.         free (symfile_fns);
  503.         symfile_fns = 0;
  504.       }
  505.       return;
  506.     }
  507.  
  508.   /* Getting new symbols may change our opinion about what is
  509.      frameless.  */
  510.   reinit_frame_cache ();
  511.  
  512.   symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
  513. }
  514.  
  515. /* Open NAME and hand it off to BFD for preliminary analysis.  Result
  516.    is a BFD *, which includes a new copy of NAME dynamically allocated
  517.    (which will be freed by the cleanup chain).  In case of trouble,
  518.    error() is called.  */
  519.  
  520. static bfd *
  521. symfile_open (name)
  522.      char *name;
  523. {
  524.   bfd *sym_bfd;
  525.   int desc;
  526.   char *absolute_name;
  527.  
  528.   name = tilde_expand (name);
  529.   make_cleanup (free, name);
  530.  
  531.   desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
  532.   if (desc < 0)
  533.     perror_with_name (name);
  534.   else
  535.     {
  536.       make_cleanup (free, absolute_name);
  537.       name = absolute_name;
  538.     }
  539.  
  540.   sym_bfd = bfd_fdopenr (name, NULL, desc);
  541.   if (!sym_bfd)
  542.     {
  543.       close (desc);
  544.       error ("Could not open `%s' to read symbols: %s",
  545.          name, bfd_errmsg (bfd_error));
  546.     }
  547.   make_cleanup (bfd_close, sym_bfd);
  548.  
  549.   if (!bfd_check_format (sym_bfd, bfd_object))
  550.     error ("\"%s\": can't read symbols: %s.",
  551.        name, bfd_errmsg (bfd_error));
  552.  
  553.   return sym_bfd;
  554. }
  555.  
  556. /* Link a new symtab_fns into the global symtab_fns list.
  557.    Called by various _initialize routines.  */
  558.  
  559. void
  560. add_symtab_fns (sf)
  561.      struct sym_fns *sf;
  562. {
  563.   sf->next = symtab_fns;
  564.   symtab_fns = sf;
  565. }
  566.  
  567.  
  568. /* Initialize to read symbols from the symbol file sym_bfd.  It either
  569.    returns or calls error().  The result is a malloc'd struct sym_fns
  570.    that contains cached information about the symbol file.  */
  571.  
  572. static struct sym_fns *
  573. symfile_init (sym_bfd)
  574.      bfd *sym_bfd;
  575. {
  576.   struct sym_fns *sf, *sf2;
  577.  
  578.   for (sf = symtab_fns; sf != NULL; sf = sf->next)
  579.     {
  580.       if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
  581.     {
  582.       sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));    
  583.       /* FIXME, who frees this? */
  584.       *sf2 = *sf;
  585.       sf2->sym_bfd = sym_bfd;
  586.       sf2->sym_private = 0;            /* Not alloc'd yet */
  587.       (*sf2->sym_init) (sf2);
  588.       return sf2;
  589.     }
  590.     }
  591.   error ("I'm sorry, Dave, I can't do that.  Symbol format unknown.");
  592.   return 0; /* Appease lint.  */
  593. }
  594.  
  595. /* This function runs the load command of our current target.  */
  596.  
  597. void
  598. load_command (arg, from_tty)
  599.      char *arg;
  600.      int from_tty;
  601. {
  602.   target_load (arg, from_tty);
  603. }
  604.  
  605. /* This function runs the add_syms command of our current target.  */
  606.  
  607. void
  608. add_symbol_file_command (args, from_tty)
  609.      char *args;
  610.      int from_tty;
  611. {
  612.   /* Getting new symbols may change our opinion about what is
  613.      frameless.  */
  614.   reinit_frame_cache ();
  615.  
  616.   target_add_syms (args, from_tty);
  617. }
  618.  
  619. /* This function allows the addition of incrementally linked object files.  */
  620.  
  621. /* ARGSUSED */
  622. void
  623. add_syms_addr_command (arg_string, from_tty)
  624.      char* arg_string;
  625.      int from_tty;
  626. {
  627.   char *name;
  628.   CORE_ADDR text_addr;
  629.   
  630.   if (arg_string == 0)
  631.     error ("add-symbol-file takes a file name and an address");
  632.  
  633.   arg_string = tilde_expand (arg_string);
  634.   make_cleanup (free, arg_string);
  635.  
  636.   for( ; *arg_string == ' '; arg_string++ );
  637.   name = arg_string;
  638.   for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
  639.   *arg_string++ = (char) 0;
  640.  
  641.   if (name[0] == 0)
  642.     error ("add-symbol-file takes a file name and an address");
  643.  
  644.   text_addr = parse_and_eval_address (arg_string);
  645.  
  646.   dont_repeat ();
  647.  
  648.   if (!query ("add symbol table from file \"%s\" at text_addr = 0x%x\n",
  649.           name, text_addr))
  650.     error ("Not confirmed.");
  651.  
  652.   symbol_file_add (name, 0, text_addr, 0);
  653. }
  654.  
  655. /* Re-read symbols if the symbol-file has changed.  */
  656. void
  657. reread_symbols ()
  658. {
  659.   struct stat symstat;
  660.  
  661.   /* With the addition of shared libraries, this should be modified,
  662.      the load time should be saved in the partial symbol tables, since
  663.      different tables may come from different source files.  FIXME.
  664.      This routine should then walk down each partial symbol table
  665.      and see if the symbol table that it originates from has been changed
  666.   */
  667.  
  668.   if (stat (symfile, &symstat) < 0)
  669.     /* Can't read symbol-file.  Assume it is up to date.  */
  670.     return;
  671.  
  672.   if (symstat.st_mtime > symfile_mtime)
  673.     {
  674.       printf_filtered ("Symbol file has changed; re-reading symbols.\n");
  675.       symbol_file_command (symfile, 0);
  676.       breakpoint_re_set ();
  677.     }
  678. }
  679.  
  680. /* This function is really horrible, but to avoid it, there would need
  681.    to be more filling in of forward references.  */
  682. void
  683. fill_in_vptr_fieldno (type)
  684.      struct type *type;
  685. {
  686.   if (TYPE_VPTR_FIELDNO (type) < 0)
  687.     {
  688.       int i;
  689.       for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
  690.     {
  691.       fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
  692.       if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
  693.         {
  694.           TYPE_VPTR_FIELDNO (type)
  695.         = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
  696.           TYPE_VPTR_BASETYPE (type)
  697.         = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
  698.           break;
  699.         }
  700.     }
  701.     }
  702. }
  703.  
  704. /* Functions to handle complaints during symbol reading.  */
  705.  
  706. /* How many complaints about a particular thing should be printed before
  707.    we stop whining about it?  */
  708.  
  709. static unsigned stop_whining = 1;
  710.  
  711. /* Print a complaint about the input symbols, and link the complaint block
  712.    into a chain for later handling.  Result is 1 if the complaint was
  713.    printed, 0 if it was suppressed.  */
  714.  
  715. int
  716. complain (complaint, val)
  717.      struct complaint *complaint;
  718.      char *val;
  719. {
  720.   complaint->counter++;
  721.   if (complaint->next == 0) {
  722.     complaint->next = complaint_root->next;
  723.     complaint_root->next = complaint;
  724.   }
  725.   if (complaint->counter > stop_whining)
  726.     return 0;
  727.   wrap_here ("");
  728.   if (!info_verbose) {
  729.     puts_filtered ("During symbol reading...");
  730.   }
  731.   printf_filtered (complaint->message, val);
  732.   puts_filtered ("...");
  733.   wrap_here("");
  734.   if (!info_verbose)
  735.     puts_filtered ("\n");
  736.   return 1;
  737. }
  738.  
  739. /* Clear out all complaint counters that have ever been incremented.  */
  740.  
  741. void
  742. clear_complaints ()
  743. {
  744.   struct complaint *p;
  745.  
  746.   for (p = complaint_root->next; p != complaint_root; p = p->next)
  747.     p->counter = 0;
  748. }
  749.  
  750. /* clear_symtab_users_once:
  751.  
  752.    This function is run after symbol reading, or from a cleanup.
  753.    If an old symbol table was obsoleted, the old symbol table
  754.    has been blown away, but the other GDB data structures that may 
  755.    reference it have not yet been cleared or re-directed.  (The old
  756.    symtab was zapped, and the cleanup queued, in free_named_symtab()
  757.    below.)
  758.  
  759.    This function can be queued N times as a cleanup, or called
  760.    directly; it will do all the work the first time, and then will be a
  761.    no-op until the next time it is queued.  This works by bumping a
  762.    counter at queueing time.  Much later when the cleanup is run, or at
  763.    the end of symbol processing (in case the cleanup is discarded), if
  764.    the queued count is greater than the "done-count", we do the work
  765.    and set the done-count to the queued count.  If the queued count is
  766.    less than or equal to the done-count, we just ignore the call.  This
  767.    is needed because reading a single .o file will often replace many
  768.    symtabs (one per .h file, for example), and we don't want to reset
  769.    the breakpoints N times in the user's face.
  770.  
  771.    The reason we both queue a cleanup, and call it directly after symbol
  772.    reading, is because the cleanup protects us in case of errors, but is
  773.    discarded if symbol reading is successful.  */
  774.  
  775. static int clear_symtab_users_queued;
  776. static int clear_symtab_users_done;
  777.  
  778. static void
  779. clear_symtab_users_once ()
  780. {
  781.   /* Enforce once-per-`do_cleanups'-semantics */
  782.   if (clear_symtab_users_queued <= clear_symtab_users_done)
  783.     return;
  784.   clear_symtab_users_done = clear_symtab_users_queued;
  785.  
  786.   printf ("Resetting debugger state after updating old symbol tables\n");
  787.  
  788.   /* Someday, we should do better than this, by only blowing away
  789.      the things that really need to be blown.  */
  790.   clear_value_history ();
  791.   clear_displays ();
  792.   clear_internalvars ();
  793.   breakpoint_re_set ();
  794.   set_default_breakpoint (0, 0, 0, 0);
  795.   current_source_symtab = 0;
  796. }
  797.  
  798. /* Delete the specified psymtab, and any others that reference it.  */
  799.  
  800. static void
  801. cashier_psymtab (pst)
  802.      struct partial_symtab *pst;
  803. {
  804.   struct partial_symtab *ps, *pprev;
  805.   int i;
  806.  
  807.   /* Find its previous psymtab in the chain */
  808.   for (ps = partial_symtab_list; ps; ps = ps->next) {
  809.     if (ps == pst)
  810.       break;
  811.     pprev = ps;
  812.   }
  813.  
  814.   if (ps) {
  815.     /* Unhook it from the chain.  */
  816.     if (ps == partial_symtab_list)
  817.       partial_symtab_list = ps->next;
  818.     else
  819.       pprev->next = ps->next;
  820.  
  821.     /* FIXME, we can't conveniently deallocate the entries in the
  822.        partial_symbol lists (global_psymbols/static_psymbols) that
  823.        this psymtab points to.  These just take up space until all
  824.        the psymtabs are reclaimed.  Ditto the dependencies list and
  825.        filename, which are all in the psymbol_obstack.  */
  826.  
  827.     /* We need to cashier any psymtab that has this one as a dependency... */
  828. again:
  829.     for (ps = partial_symtab_list; ps; ps = ps->next) {
  830.       for (i = 0; i < ps->number_of_dependencies; i++) {
  831.     if (ps->dependencies[i] == pst) {
  832.       cashier_psymtab (ps);
  833.       goto again;        /* Must restart, chain has been munged. */
  834.     }
  835.       }
  836.     }
  837.   }
  838. }
  839.  
  840. /* If a symtab or psymtab for filename NAME is found, free it along
  841.    with any dependent breakpoints, displays, etc.
  842.    Used when loading new versions of object modules with the "add-file"
  843.    command.  This is only called on the top-level symtab or psymtab's name;
  844.    it is not called for subsidiary files such as .h files.
  845.  
  846.    Return value is 1 if we blew away the environment, 0 if not.
  847.  
  848.    FIXME.  I think this is not the best way to do this.  We should
  849.    work on being gentler to the environment while still cleaning up
  850.    all stray pointers into the freed symtab.  */
  851.  
  852. int
  853. free_named_symtabs (name)
  854.      char *name;
  855. {
  856.   register struct symtab *s;
  857.   register struct symtab *prev;
  858.   register struct partial_symtab *ps;
  859.   struct blockvector *bv;
  860.   int blewit = 0;
  861.  
  862.   /* Some symbol formats have trouble providing file names... */
  863.   if (name == 0 || *name == '\0')
  864.     return 0;
  865.  
  866.   /* Look for a psymtab with the specified name.  */
  867.  
  868. again2:
  869.   for (ps = partial_symtab_list; ps; ps = ps->next) {
  870.     if (!strcmp (name, ps->filename)) {
  871.       cashier_psymtab (ps);    /* Blow it away...and its little dog, too.  */
  872.       goto again2;        /* Must restart, chain has been munged */
  873.     }
  874.   }
  875.  
  876.   /* Look for a symtab with the specified name.  */
  877.  
  878.   for (s = symtab_list; s; s = s->next)
  879.     {
  880.       if (!strcmp (name, s->filename))
  881.     break;
  882.       prev = s;
  883.     }
  884.  
  885.   if (s)
  886.     {
  887.       if (s == symtab_list)
  888.     symtab_list = s->next;
  889.       else
  890.     prev->next = s->next;
  891.  
  892.       /* For now, queue a delete for all breakpoints, displays, etc., whether
  893.      or not they depend on the symtab being freed.  This should be
  894.      changed so that only those data structures affected are deleted.  */
  895.  
  896.       /* But don't delete anything if the symtab is empty.
  897.      This test is necessary due to a bug in "dbxread.c" that
  898.      causes empty symtabs to be created for N_SO symbols that
  899.      contain the pathname of the object file.  (This problem
  900.      has been fixed in GDB 3.9x).  */
  901.  
  902.       bv = BLOCKLIST (s);
  903.       if (BLOCKLIST_NBLOCKS (bv) > 2
  904.       || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
  905.       || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
  906.     {
  907.       complain (&oldsyms_complaint, name);
  908.  
  909.       clear_symtab_users_queued++;
  910.       make_cleanup (clear_symtab_users_once, 0);
  911.       blewit = 1;
  912.     } else {
  913.       complain (&empty_symtab_complaint, name);
  914.     }
  915.  
  916.       free_symtab (s);
  917.     }
  918.   else
  919.     /* It is still possible that some breakpoints will be affected
  920.        even though no symtab was found, since the file might have
  921.        been compiled without debugging, and hence not be associated
  922.        with a symtab.  In order to handle this correctly, we would need
  923.        to keep a list of text address ranges for undebuggable files.
  924.        For now, we do nothing, since this is a fairly obscure case.  */
  925.     ;
  926.  
  927.   /* FIXME, what about the misc function vector? */
  928.   return blewit;
  929. }
  930.  
  931. void
  932. _initialize_symfile ()
  933. {
  934.  
  935.   add_com ("symbol-file", class_files, symbol_file_command,
  936.        "Load symbol table from executable file FILE.\n\
  937. The `file' command can also load symbol tables, as well as setting the file\n\
  938. to execute.");
  939.  
  940.   add_com ("add-symbol-file", class_files, add_symbol_file_command,
  941.    "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
  942. The second argument provides the starting address of the file's text.");
  943.  
  944.   add_com ("load", class_files, load_command,
  945.    "Dynamically load FILE into the running program, and record its symbols\n\
  946. for access from GDB.");
  947.  
  948.   add_show_from_set
  949.     (add_set_cmd ("complaints", class_support, var_uinteger,
  950.           (char *)&stop_whining,
  951.       "Set max number of complaints about incorrect symbols.",
  952.           &setlist),
  953.      &showlist);
  954.  
  955.   obstack_init (symbol_obstack);
  956.   obstack_init (psymbol_obstack);
  957. }
  958.